package com.ruoyi.project.module.fbsDetail.service;

import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.module.fbsDetail.domain.FbsDetail;
import com.ruoyi.project.module.fbsDetail.mapper.FbsDetailMapper;
import com.ruoyi.project.module.fbsHead.domain.FbsHead;
import com.ruoyi.project.module.fbsHead.mapper.FbsHeadMapper;
import com.ruoyi.project.module.products.service.ProductsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ruoyi.common.utils.StringUtils.IntegerToNum;

/**
 * FBS明细 服务层实现
 *
 * @author lcx
 * @date 2018-09-17
 */
@Service
public class FbsDetailServiceImpl implements IFbsDetailService {
    @Autowired
    private FbsDetailMapper fbsDetailMapper;
    @Autowired
    private FbsHeadMapper fbsHeadMapper;
    @Autowired
    ProductsServiceImpl productsService;

    /**
     * 查询FBS明细信息
     *
     * @param id FBS明细ID
     * @return FBS明细信息
     */
    @Override
    public FbsDetail selectFbsDetailById(Integer id) {
        return fbsDetailMapper.selectFbsDetailById(id);
    }

    /**
     * 查询FBS明细列表
     *
     * @param fbsDetail FBS明细信息
     * @return FBS明细集合
     */
    @Override
    public List<FbsDetail> selectFbsDetailList(FbsDetail fbsDetail) {
        if (StringUtils.isNotEmpty(fbsDetail.getSearchValue())) {
            String[] searchRow = {
                    "f1.Name",
                    "f1.TFileUrl",
                    "f2.SKU",
                    "p.productName",
                    "f2.ChineseName"

            };
            return fbsDetailMapper.selectFbsDetailListAsSearchRow(fbsDetail, searchRow);
        } else {
            return fbsDetailMapper.selectFbsDetailList(fbsDetail);
        }
    }

    /**
     * @Author lcx
     * @Date 2018/9/26 15:23
     * 计算合计
     */
    @Override
    public List<FbsDetail> selectSumCountFbsDetailList(FbsDetail fbsDetail) {
        if (StringUtils.isNotEmpty(fbsDetail.getSearchValue())) {
            String[] searchRow = {
                    "f1.Name",
                    "f1.TFileUrl",
                    "f2.SKU",
                    "p.productName",
                    "f2.ChineseName"

            };
            return fbsDetailMapper.selectFbsDetailListAsSearchRowAsSum(fbsDetail, searchRow);
        } else {
            return fbsDetailMapper.selectFbsDetailListAsSum(fbsDetail);
        }
    }

    /**
     * 新增FBS明细
     *
     * @param fbsDetail FBS明细信息
     * @return 结果
     */
    @Override
    public int insertFbsDetail(FbsDetail fbsDetail) {
        return fbsDetailMapper.insertFbsDetail(fbsDetail);
    }

    /**
     * 修改FBS明细
     *
     * @param fbsDetail FBS明细信息
     * @return 结果
     */
    @Override
    public int updateFbsDetail(FbsDetail fbsDetail) {
        return fbsDetailMapper.updateFbsDetail(fbsDetail);
    }

    /**
     * 删除FBS明细对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFbsDetailByIds(String ids) {
        return fbsDetailMapper.deleteFbsDetailByIds(Convert.toStrArray(ids));
    }

    @Override
    public List<FbsDetail> selectFbsHeadListAsEditReceived(FbsDetail fbsDetail) {
        return fbsDetailMapper.selectFbsDetailList(fbsDetail);
    }

    @Override
    public int updatePurchasesAsReceived(String[] ids, String[] actualQuantity, String[] headIds) {
        int i=0;
        int re_tem = 0;
        for (String id : ids) {
            //查询之前的实际数量
            FbsDetail fbsDetail_tem = fbsDetailMapper.selectFbsDetailById(Integer.valueOf(id));
            int lost_num = Integer.valueOf(actualQuantity[i]) - IntegerToNum(fbsDetail_tem.getActualQuantity());

            FbsDetail fbsDetail = new FbsDetail();
            fbsDetail.setId(Integer.valueOf(id));
            fbsDetail.setActualQuantity(Integer.valueOf(actualQuantity[i]));

            re_tem += fbsDetailMapper.updateFbsDetail(fbsDetail);

            productsService.uploadProductAsidAndType(fbsDetail_tem.getProductId(), lost_num,"FbsIntoQuantity");

            i++;
        }
        for (String headid : headIds) {
            FbsHead fbsHead = new FbsHead();

            fbsHead.setId(headid);
            fbsHead.setStatus(String.valueOf(2));
            fbsHeadMapper.updateFbsHead(fbsHead);

        }

        return re_tem;
    }


}
